package university;

import university.exceptions.InvalidMajorScholarshipException;
import university.exceptions.InvalidScholarshipException;
import university.exceptions.ListEmptyException;
import university.exceptions.NoElementInListException;

/**
 * Class that analyzes resuls of session, removes students from group. Assigns scholarship bonus for most successful students
 * @author anatolii.bondarenko.
 */
class SessionResults {
    private double basicScholarship;
    private double majorScholarship;
    /**
     * Set ammount of basic and major scholarship for current semester
     * @param bs is ammount of basic scholarship
     * @param ms is ammount of major scholarship
     */
    public SessionResults(double bs, double ms)throws InvalidScholarshipException, InvalidMajorScholarshipException{
        basicScholarship = bs;
        majorScholarship = ms;
        if (bs < 0 || ms<0){
            throw new InvalidScholarshipException("Scholarship cannot be negative");
        }
        if (ms < bs){
            throw new InvalidMajorScholarshipException("Major scholarship cannot be less than basic scholarship");
        }
    }

    /**
     * Checks if there are resigned students in group. If they are present then remove them from group.
     * @param group is group to check
     */
    public static void removeResignedStudents(Group group) throws ListEmptyException, NoElementInListException{
        for(int i = 0; i < group.getListOfStudents().size(); i++){
            Student s=group.getListOfStudents().get(i);
            if (s.isResigned()){
                group.removeStudentFromGroup(s);
                i--;
            }
        }
    }

    /**
     * Analyzes assessments of students. If they are good enough then assigns scholarship to students.
     * @param group is group for analysis
     */
    public void scholarshipForStudents(Group group) throws ListEmptyException, NoElementInListException, InvalidScholarshipException{
        removeResignedStudents(group);
        for (Student s:group.getListOfStudents()){
            //Find average assessment of student (sum of all assessments divided by quantity of exams)
            int averageAssessment = 0;
            for (int assessment:s.getExamAssessments()){
                averageAssessment += assessment;
            }
            averageAssessment /= s.getExamAssessments().size();

            //if avarage assessment is 5 than student receives major scholarship bonus
            if (averageAssessment == 5){
                s.setScholarship(majorScholarship);
            }
            //if average assessment is higher than 4 then student receives basic scholarship bonus
            else if(averageAssessment<5 && averageAssessment>=4){
                s.setScholarship(basicScholarship);
            }
            //if average assessment is lower than 4 then student receives no assessment bonus
            else{
                s.setScholarship(0);
            }
        }
    }

}
